Imports System.IO
Imports System.Text.RegularExpressions
Imports System.text
Imports System.xml

Public Class CreateTags
    Private xmlText As XmlDocument
    Private arSiteTemplate As ArrayList = New ArrayList
    Private arSiteName As ArrayList = New ArrayList

    Private Sub LoadXML()
        Try

            xmlText = New XmlDocument
            xmlText.Load(My.Computer.FileSystem.CurrentDirectory & "\Plugins\TagTemplates.xml")

        Catch ex As Exception

            Dim mylog As New LogFile
            mylog.WriteToLog(ex.Message.ToString & " " & ex.InnerException.ToString)

        End Try

    End Sub

    Private Sub LoadSites()

        Try
            arSiteTemplate.Clear()
            arSiteName.Clear()

            LoadXML()

            'Creating af new arraylist to hold my sites
            InsertSmartTags.arSites = New ArrayList
            InsertSmartTags.arSites.Clear()


            Dim myNode As XmlNode = Nothing
            For Each myNode In xmlText.DocumentElement

                If myNode("inUse").InnerText.ToString = "true" Then

                    InsertSmartTags.arSites.Add(myNode.Attributes("name").Value.ToString)
                    'InsertSmartTags.mySitesTags.SetString(myNode.Attributes("name").Value.ToString, InsertSmartTags.myTags)

                    arSiteTemplate.Add(myNode("tagFormat").InnerText.ToString)
                    arSiteName.Add(myNode.Attributes("name").Value.ToString)

                End If

            Next

        Catch ex As Exception

            Dim mylog As New LogFile
            mylog.WriteToLog(ex.Message.ToString & " " & ex.InnerException.ToString)

        End Try

    End Sub

    Public Function CreateTags(ByVal Text As String) As String
        Try

            If TrimTags(Text.Trim).Trim.Length = 0 Then

                Return String.Empty

            Else

                LoadSites()

                Dim tagBuilder As StringBuilder = New StringBuilder
                Dim tagHTML As StringBuilder = New StringBuilder

                Dim tagTemp As String = String.Empty
                Dim htmlTemp As String = String.Empty

                Dim urltags As String() = Regex.Replace(TrimTags(Text).Trim, ",\s", ",").Replace(" ", "+").Split(",".ToCharArray)
                Dim displaytags As String() = Regex.Replace(TrimTags(Text).Trim, ",\s", ",").Split(",".ToCharArray)

                Dim iTags As Integer = 0
                Dim iTemplates As Integer = 0

                'tagHTML.Append("<!-- Begin Auto Generated Tags -->")
                tagHTML.Append("<p>")

                Do While (iTemplates < arSiteTemplate.Count)
                    htmlTemp = Regex.Replace(My.Settings.HtmlTemplate.ToString, "{link}", "{1}", RegexOptions.IgnoreCase)
                    htmlTemp = Regex.Replace(htmlTemp, "{SiteName}", "{0}", RegexOptions.IgnoreCase)
                    tagTemp = Regex.Replace(arSiteTemplate.Item(iTemplates).ToString, "{tag}", "{0}")
                    tagTemp = Regex.Replace(tagTemp.ToString, "{displaytag}", "{1}")

                    Do While (iTags < urltags.Length)

                        If (tagBuilder.Length > 0) Then
                            tagBuilder.Append(", ")
                        End If
                        tagBuilder.Append(String.Format(tagTemp, urltags(iTags).Trim.ToString, displaytags(iTags).Trim.ToString))
                        iTags += 1
                    Loop

                    tagHTML.Append(String.Format(htmlTemp, arSiteName(iTemplates).ToString, tagBuilder.ToString))
                    tagHTML.Append("<br />")

                    tagBuilder.Remove(0, tagBuilder.Length)

                    tagTemp = String.Empty

                    iTemplates += 1
                    iTags = 0

                Loop
                tagHTML.Append("<!-- StartInsertedTags: " & TrimTags(Text.Trim).Trim & " :EndInsertedTags -->")
                tagHTML.Append("</p>")
                'tagHTML.Append("<!-- End Auto Generated Tags -->")

                Return tagHTML.ToString

            End If

        Catch ex As Exception

            Dim mylog As New LogFile
            mylog.WriteToLog(ex.Message.ToString & " " & ex.InnerException.ToString)
            Return String.Empty

        End Try
    End Function

    Private Function TrimTags(ByRef strTags As String) As String
        Try
            'Creating a string to hold the tags
            Dim strReturn As String = strTags

            'Looping through the string and removing trailing commas
            Do While strReturn.Trim.EndsWith(",")
                strReturn = strReturn.Remove(strReturn.LastIndexOf(","), 1).Trim
            Loop

            'Returning the string without trailing commas
            Return strReturn.ToString

        Catch ex As Exception

            Dim mylog As New LogFile
            mylog.WriteToLog(ex.Message.ToString & " " & ex.InnerException.ToString)
            Return Nothing

        End Try


    End Function

End Class
